× About Us How it works Pricing Student Archive Subjects Blog Contact Us

Enrich your knowledge with our informative blogs

Types of Loops in programming? 

Loops: Loops are elements that help in executing repeated code in minimum lines of code. The loop is run till a specified condition and the control exits from the loop when the condition becomes false.  

Parts of a Loop  

There are two parts of a loop: 

1). Block of statements or body  

2). Conditions that control the loop 

There are three essential statements that need to be taken care of, else the loop will either won’t work or enter into an infinite loop.  

These are:  

  • Initialization condition 
  • Test condition/expression 
  • Increment/determent/update condition  

Before moving on to types of loops, understand  

Why do we need Loops?
If you want to print 1 to 5, there are two ways to do that. 

Method 1 Iterative Method  

init
main()
{
  printf (“1\n”);
  printf (“2\n”);
  printf (“3\n”);
  printf (“4\n”);
  printf (“5\n”);
  return 0;
}

Here you need to give the print statement 5 times, one each for every number.  

Why to avoid this approach? 

  • Makes the code look messy. 
  • Increase Lines of code and lacks optimization  
  • The code is difficult to optimize  
  • Considered as bad programming practice  
  • Difficult to debug 

All these problems are solved by using loops. 

Method 2 Loops 

Loops are used when you need to repeatedly execute the block of statements. They use minimum lines and deliver maximum efficiency.  

Loops make the code look simple, easy and well optimized.  

Loops fall under two types of categories: 

1). Entry controlled Loops: 

Here the test condition is tested before sending the control of the program inside the loop. 

The body of the loop is executed only if the condition turns out to be true. For example: while loop, for loop 

2). Exit Controlled Loops   

Here, the body of the loop is executed atleast once and the test condition is checked after the end of the loop. So, irrespective that the condition is true or false, the loop gets executed. For example: do-while loop 

Now, Let’s discuss how to print 1 to 5 or even beyond that by using different types of loops. 

Types of Loops 

1. While Loop 

Generally ‘while’ loop is used when we don’t know about the number of iterations. The condition is tested before entering the loop and the body of the loop gets executed only when the condition turns out to be true. 

Syntax: 

initilization_condition;
while(test_condition){
  Body_of_the_loop;
  update_condition;
}

Working of while Loop: 

  • The control/test variable is initialized. 
  • If the test condition is true, body of the loop is executed.  
  • After that, the test condition gets updated and the control gets shifted again to the test condition.  
  • The loop keeps on repeating till the test condition turns false.  

Here’s how while loop will be used to print 1 to 5. 

#include<studio.h>
int
main()
{
  int i=1;       // initilization
  while(i<6) {                  // checking condition
    printf (“%d\n”, i);
    i = i+1;                      // Incrementing the condition
  }
  return 0;
}
2. For Loop 

When you know the number of iterations, it always better to use for loop. It not only helps to execute the statements multiple times in one go but also reduces LOC (Lines of code) significantly.  

It allows performing initialization, testing condition and updating expression in just one line.

Syntax: 

for (initilization expr; test_condition; update_exp) {
  //body_of_the_loop
}

 

How for loop works? 

  • The moment control of the program reaches for loop, initialization is performed. This statement is run just once in the entire cycle of ‘for’ loop.  
  • After initialization, test condition is checked. If the test condition is true then the body of the loop is executed.  
  • The control switches again to the first statement and the update condition is triggered. Next, test-condition is checked again.   
  • The loop continues till the test condition turns false.  

Let’s how to print 1 to 5 using a for loop: 

main()
{
  for (int i=1; i<=5; i++){
    printf (“%d\n”, i);
  }
  return 0;
}

3. Do-while loop 

This loop is just like while loop with just a change that the testing condition is checked at the end of the loop.  

SYNTAX: 

initilization_condition;
do
{
  Body_of_the_loop;
  update_condition;
} while (text-expr);

How does a do-while loop works? 

  • The counter/test variable is initialized. 
  • The control directly jumps to the body of the loop and all the statements are executed.  
  • The update condition is performed. 
  • Based on this update condition, test expression is now checked. If the expression is true, the control is shifts to the start of the loop again.  
  • The loop ends when the test condition becomes falls. But, one thing is for sure, the code will run atleast once.  

Example: Let’s learn how to write 1 to 5 using the do-while loop. 

main()
{
  int i=1;
  do{
      printf (“%d\n”,i);
      i= i+1;
  } while (i<6);
  return 0;
}
Unveil your gateway to a lucrative career!
Unveil your gateway to a lucrative career!

Unleash the power of true logic building with Real-time instructions and live coding exposure.

Book A Demo Class

Tel Guru
Tel Guru

Register For The Demo Class

[footer-form]